package com.aegean.MonitorExamSYS.examinee.business.impl;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aegean.MonitorExamSYS.examinee.business.ExamineeBusiness;
import com.aegean.MonitorExamSYS.examinee.component.ExamineeExam;
import com.aegean.MonitorExamSYS.examinee.dao.ExamineeDao;
import com.aegean.MonitorExamSYS.tools.MyTools;
import com.aegean.MonitorExaxSYS.model.BigType;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.ExamineeQualification;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.SmallType;
import com.aegean.MonitorExaxSYS.model.component.Photo;

@Service
@Transactional
public class ExamineeBusinessImpl implements ExamineeBusiness {
	@Resource
	ExamineeDao dao;

	@Override
	public ExamineeValue insertUser(ExamineeValue one) {
		// TODO Auto-generated method stub
		return dao.insertUser(one);
	}

	@Override
	public ExamineeValue insertUserLocation(ExamineeValue one, float Lat,
			float Lng, String address, String route, String city, String region) {
		// TODO Auto-generated method stub
		return dao.insertUserLocation(one, Lat, Lng, address, route, city,
				region);
	}

	@Override
	public ExamineeValue loginUser(String user_Login, String user_Pwd) {
		// TODO Auto-generated method stub
		return dao.loginUser(user_Login, user_Pwd);
	}

	@Override
	public ExamineeValue updateUser(int Customer_Id, ExamineeValue one,
			Photo photo) {
		// TODO Auto-generated method stub
		return dao.updateUser(Customer_Id, one, photo);
	}

	@Override
	public Blob showPhoto(int Customer_Id) {
		// TODO Auto-generated method stub
		return dao.showPhoto(Customer_Id);
	}

	@Override
	public ExamineeValue updateUserPwd(int Customer_Id, String newPwd) {
		// TODO Auto-generated method stub
		return dao.updateUserPwd(Customer_Id, newPwd);
	}

	@Override
	public ExamineeValue updateUserLocation(ExamineeValue one, float Lat,
			float Lng, String address, String route, String city, String region) {
		// TODO Auto-generated method stub
		return dao.updateUserLocation(one, Lat, Lng, address, route, city,
				region);
	}

	@Override
	public int searchExamValueCount(int small_Id) {
		// TODO Auto-generated method stub
		return dao.searchExamValueCount(small_Id);
	}

	@Override
	public List<ExamInfo> searchPartExamValues(int small_Id, int start,
			int limit) {
		// TODO Auto-generated method stub
		return dao.searchPartExamValues(small_Id, start, limit);
	}

	@Override
	public List<ConductionValue> showConducation(int exam_Id, double x, double y) {
		// TODO Auto-generated method stub
		List<ConductionValue> ones = dao.showConducation(exam_Id);
		int j;
		for (int i = 1; i < ones.size(); i++) {
			ConductionValue temp = ones.get(i);
			j = i;
			while (j > 0
					&& MyTools.GetDistance(ones.get(j - 1)
							.getConduction_Address().getPlace_H(),
							ones.get(j - 1).getConduction_Address()
									.getPlace_V(), x, y) > MyTools
							.GetDistance(temp.getConduction_Address()
									.getPlace_H(), temp.getConduction_Address()
									.getPlace_V(), x, y)) {
				ones.set(j, ones.get(j - 1));
				--j;
			}
			ones.set(j, temp);
		}
		/*for (ConductionValue co : ones) {
			System.out.println(co.getConduction_Name()
					+ ":"
					+ MyTools.GetDistance(co.getConduction_Address()
							.getPlace_H(), co.getConduction_Address()
							.getPlace_V(), x, y));
		}*/
		return ones;
	}

	@Override
	public boolean applyQualification(int conduction_Id, int examinee_Id,
			int examInfo_Id) {
		// TODO Auto-generated method stub
		return dao.applyQualification(conduction_Id, examinee_Id, examInfo_Id);
	}

	@Override
	public boolean validationExam(int exam_Id, Date startTime, Date endTime,
			int user_Id) {
		// TODO Auto-generated method stub
		int count = dao.validationExam(exam_Id, startTime, endTime,
				user_Id);
		if (count == 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public int showExamineeQualifications(int user_Id,String state) {
		// TODO Auto-generated method stub
		return dao.showExamineeQualifications(user_Id,state);
	}

	@Override
	public List<ExamineeExam> showExamineeQualificationsList(
			int user_Id, int start, int limit,String state) {
		// TODO Auto-generated method stub
		return dao.showExamineeQualificationsList(user_Id, start, limit,state);
	}

	@Override
	public boolean validateUser(String user_Login) {
		// TODO Auto-generated method stub
		return dao.validateUser(user_Login);
	}

	@Override
	public int countOfcurrentAppliedPersonCount(int exam_Id, int conduction_Id) {
		// TODO Auto-generated method stub
		return dao.countOfcurrentAppliedPersonCount(exam_Id, conduction_Id);
	}

	@Override
	public int countOfcurrentExamAvailablePersonCount(int conduction_Id,
			String start_Time, String end_Time) {
		// TODO Auto-generated method stub
		return dao.countOfcurrentExamAvailablePersonCount(conduction_Id, start_Time, end_Time);
	}

}
